package com.guoxue.auth;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import com.guoxue.dao.impl.TapesRecordsDaoImpl;
import com.guoxue.dao.impl.UserDaoImpl;
import com.guoxue.domain.BackUserVo;
import com.guoxue.domain.TapesRecordsVo;
import com.guoxue.domain.UserVo;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OSSUtil;




public class AuthFilter extends OncePerRequestFilter{
	String basePath;
	private UserDaoImpl userDao;

    @Override
    protected void initFilterBean() throws ServletException {
    	super.initFilterBean();
    	ServletContext servletContext = getServletContext();
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		this.userDao = (UserDaoImpl) webApplicationContext.getBean("userDaoImpl");
    }
	@Override
	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
			throws ServletException, IOException {
		String requestUrl = req.getRequestURI();
		if (requestUrl.indexOf(".do")<=0||requestUrl.indexOf("login.do")>0||requestUrl.indexOf("logout.do")>0
				||requestUrl.indexOf("register.do")>0  ||requestUrl.indexOf("/back/logout.do")>0 
				||requestUrl.indexOf("/back/login.do")>0 || requestUrl.indexOf("captcha-image.do")>0||requestUrl.indexOf("/User")>0){
			chain.doFilter(req, resp);
			return;
		}
		HttpSession session = req.getSession();
		BackUserVo backuser = (BackUserVo) session.getAttribute("Back_user");
		if (backuser!=null){
			chain.doFilter(req, resp);
			return;
		}
		UserVo user = (UserVo) session.getAttribute("user");
		if (user==null){
			resp.getWriter().write("No Permission Please Go Login");
			return;
		} else {
			chain.doFilter(req, resp);
			return;
		}
	}
}
